<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: songaimin@clcw.com.cn  Blog:http://blog.csdn.net/samxx8        |
// +------------------------------------------------------------------------+
//
namespace AuctionCore\Other;

use AuctionCore\AntNest\UserCenter;
use AuctionCore\CarDealer\Dealer;
/**
 * 拉卡拉对账类
 * @author angl
 */
class LakalaBill
{
    private $model = null;

    public function __construct()
    {
        $this->model = M('lakala_bill');
    }

    /**
     *  添加对账记录
     * @param array $data
     * @return int last_insert_id
     */
    public static function add($data)
    {
        $data['create_time'] = date("Y-m-d H:i:s");
        return M('lakala_bill')->add($data);
    }

    public static function add_logs($data_list)
    {
        return M('lakala_bill_log')->addAll($data_list);
    }

    //获取list
    public function getList($page = 1, $page_num = 10, $params = array())
    {
        $page = (int)$page < 1 ? 1 : (int)$page;
        $page_num = (int)$page_num < 1 ? 10 : (int)$page_num;

        $map = array();

        if (!empty($params['start_date']) || !empty($params['end_date'])) {
            if (empty($params['start_date'])) {
                $map['create_date'] = array("elt", $params['end_date']);
            } elseif (empty($params['end_date'])) {
                $map['create_date'] = array("egt", $params['start_date']);
            } else {
                $map['create_date'] = array(array("egt", $params['start_date']), array("elt", $params['end_date']));
            }
        }

        return $this->model->where($map)->page($page)->limit($page_num)->order('create_date desc')->select();

    }

    //获取总数
    public function getTotal($params)
    {
        $map = array();
        if (!empty($params['start_date']) || !empty($params['end_date'])) {
            if (empty($params['start_date'])) {
                $map['create_date'] = array("elt", $params['end_date']);
            } elseif (empty($params['end_date'])) {
                $map['create_date'] = array("gt", $params['start_date']);
            } else {
                $map['create_date'] = array(array("gt", $params['start_date']), array("elt", $params['end_date']));
            }
        }

        return $this->model->where($map)->count();
    }

    /**
     * 每日充值详情
     * @param int $page
     * @param int $limit
     * @param $date
     * @param string $user_name
     * @param string $real_name
     * @param string $mobile
     * @param int $bc_id
     * @return array
     */
    public function getDetailList($page = 1, $limit = 10, $date, $dealer_id, $real_name = '', $mobile = '', $bc_id = -1)
    {
        $page = (int)$page < 1 ? 1 : (int)$page;
        $limit = (int)$limit < 1 ? 10 : (int)$limit;
        $map = array();
        $map['rl.third_party'] = 1;
        $map['rl.deal_time'] = [['egt', $date . " 00:00:00"], ['elt', $date . " 23:59:59"]];
        //车商ID
        if ($dealer_id > 0) {
            $map['d.dealer_id'] = $dealer_id;
        }
        //联系人
        if (!empty($real_name)) {
            $map['d.contact_person'] = $real_name;
        }
        //联系人电话
        if (!empty($mobile)) {
            $uc = new UserCenter();
            $info1 = $uc->info_bymobile($mobile,2,1);
            $map['du.uid'] = $info1['account_id'];
            //$map['d.contact_mobile'] = $mobile;
        }
        //分公司
        if ($bc_id > -1) {
            $map['d.depart_id'] = $bc_id;
        }
        $prefix = C('DB_PREFIX');
        $cols = 'd.dealer_id,d.contact_person,du.uid,rl.*,dp.name as branch_name';
        $data = [];
        $data['total'] = M('recharge_log')->alias('rl')
            ->join($prefix . 'car_dealer as d ON rl.dealer_id=d.dealer_id')
            ->join($prefix . "dealer_user as du ON d.dealer_id=du.dealer_id")
            ->join("LEFT JOIN " . $prefix . 'depart as dp ON dp.id=d.depart_id')
            ->where($map)->count();
        $data['list'] = M('recharge_log')->alias('rl')->field($cols)
            ->join($prefix . 'car_dealer as d ON rl.dealer_id=d.dealer_id')
            ->join($prefix . "dealer_user as du ON d.dealer_id=du.dealer_id")
            ->join("LEFT JOIN " . $prefix . 'depart as dp ON dp.id=d.depart_id')
            ->where($map)->page($page)->limit($limit)->order('rl.deal_time desc')->select();
        return $data;
    }
}

//end class
